using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ENTITIES;

namespace DAL
{
   public class DoanhNghiepDAL
    {
        private SqlConnection cn = null;
        public DoanhNghiepDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TBDoanhNhiep dn)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDN",dn.tendn);
            cm.Parameters.AddWithValue("@TaiKhoan", dn.taikhoan);
            cm.Parameters.AddWithValue("@MatKhau",dn.matkhau);
            cm.Parameters.AddWithValue("@DiaChi",dn.diachi);
            cm.Parameters.AddWithValue("@Website",dn.website);
            cm.Parameters.AddWithValue("@SoDT",dn.sodt);
            cm.Parameters.AddWithValue("@Email",dn.email);
           
            cm.ExecuteNonQuery();
        }
        public void Update(TBDoanhNhiep dn)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", dn.madn);
            cm.Parameters.AddWithValue("@TenDN", dn.tendn);
            cm.Parameters.AddWithValue("@TaiKhoan", dn.taikhoan);
            cm.Parameters.AddWithValue("@MatKhau", dn.matkhau);
            cm.Parameters.AddWithValue("@DiaChi", dn.diachi);
            cm.Parameters.AddWithValue("@Website", dn.website);
            cm.Parameters.AddWithValue("@SoDT", dn.sodt);
            cm.Parameters.AddWithValue("@Email", dn.email);
            
          
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", iD);
            cm.ExecuteNonQuery();
        }

        public tbDoanhNghiepCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDoanhNghiepCollection doanhnghiepCollection = new tbDoanhNghiepCollection();

            while (dr.Read())
            {
                TBDoanhNhiep doanhnghiep = new TBDoanhNhiep();
                doanhnghiep.madn = int.Parse(dr.GetValue(0).ToString());
                doanhnghiep.tendn = dr.GetValue(1).ToString();
                doanhnghiep.taikhoan = dr.GetValue(2).ToString();
                doanhnghiep.matkhau = dr.GetValue(3).ToString();
                doanhnghiep.diachi = dr.GetValue(4).ToString();
                doanhnghiep.website = dr.GetValue(5).ToString();
                doanhnghiep.sodt = dr.GetValue(6).ToString();
                doanhnghiep.email = dr.GetValue(7).ToString();
             
                doanhnghiepCollection.Add(doanhnghiep);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return doanhnghiepCollection;
        }
    }
}
